Object-Oriented Programming
Pluto makes a plethora of improvements when it comes to object-oriented programming.
Method Creation
A series of methods was otherwise ugly to implement into a Lua table.
Old Codeplutolocal t = {}function t:f1(...) endfunction t:f2(...) endfunction t:f3(...) end
Now, you can inline these statements inside of your table.
New Codeplutolocal t = {function f1() end,function f2() end,function f3() end}
This automatically marks the functions as methods.
Keep in mind, this produces methods, meaning you'll need to use the colon invocation syntax to avoid positional ambiguities in your parameters.
Static Functions
For consistency with the above syntax, you can use 'static function' to declare non-method functions in your tables:
plutolocal t = {static function say(msg)print(msg)end}t.say("Hello") --> Hello
New Operator
Pluto adds an easy way to make instances with the new operator. This operator will also call the __construct method if it exists.
plutolocal Human = {function __construct(name)self.name = nameend}local john = new Human("John")print(john.name) --> John
Note that for compatibility with Lua and C API classes, the new operator checks for the existence of a static new function. If it exists, new X(...) will be identical to X.new(...). In contrast to __construct, there is no 'self' argument given to new.
Class Statement
The class statement is similar to a table constructor, but it does not require commas or semicolons:
plutoclass Humanfunction __construct(name)self.name = nameendfunction __gc()print(self.name .. " died of irrelevance")endendlocal john = new Human("John")
The class statement also supports modifiers: local class NAME, export class NAME
Class Expression
There is also a class expression, which can be used e.g. when assigning to a variable:
plutolocal Human = classfunction __construct(name)self.name = nameendend
Extends
The class statement also supports extends to specify a parent:
plutoclass Entityage = 1endclass Human extends Entityendlocal human = new Human()print(human.age) --> 1
This also adds a __parent field to Human.
Parent Expression
The parent expression is a shorthand for self.__parent, which also supports method call syntax, in which case it's a shorthand for self.__parent.METHOD(self, ...).
plutoclass Entityfunction __construct(name)self.name = nameendendclass Human extends Entity-- If we don't define __construct, the parent-constructor would be called automatically.-- However, if we overwrite it, we can use parent:__construct to call it manually.function __construct(name)parent:__construct(name)endendlocal human = new Human("John")print(human.name) --> John
Note that if you have a local variable (or function parameter) called "parent", the parent expression will defer to it.
Restricted Fields
Pluto allows you to specify if a field is 'public', 'protected' or 'private'. Private fields can only be accessed by the class that defined them. Protected fields can additionally also be accessed by descendant classes.
plutoclass Vehiclefunction __construct(protected manufacturer)endendclass Car extends Vehiclefunction __construct(manufacturer, private model, public year)parent:__construct(manufacturer)endfunction getName()return $"{self.year} {self.manufacturer} {self.model}"endendlocal cor = new Car("Toyota", "Corolla", 2025)print(cor.manufacturer) --> nilprint(cor.model) --> nilprint(cor.year) --> 2025print(cor:getName()) --> 2025 Toyota Corolla
Constructor Promotion
Because a common task of __construct methods is to assign the value of arguments to table fields, Pluto provides a simple syntax to reduce this boilerplate:
plutoclass Humanfunction __construct(public name, private age)endfunction getAge()return self.ageendendlocal human = new Human("John", 42)print(human.name) -- "John"print(human:getAge()) -- 42print(human.age) -- nil
Instanceof Operator
The instanceof operator can be used to check if a table is a class instance, including inherited classes:
plutoclass Entity endclass Human extends Entity endlocal e = new Entity()local h = new Human()print(e instanceof Entity) -- trueprint(e instanceof Human) -- falseprint(h instanceof Entity) -- true (by inheritance)print(h instanceof Human) -- true
It can also be used as a function:
plutoclass Entity endlocal e = new Entity()print(instanceof(e, Entity)) -- true
Try It Yourself
Note that, while the instanceof operator generates Lua-compatible bytecode, the instanceof function is a part of Pluto's standard library, and hence unavailable under Lua.
Using Compatibility Mode?
Some of the syntax discussed here may be different due to compatiblity mode:
new->pluto_newclass->pluto_classparent->pluto_parent
Alternatively, pluto_use new, class, parent will enable these keywords independently of environment settings.